package com.zmc.lostfound.auth.controller;

import com.zmc.lostfound.auth.service.RolePermissionService;
import com.zmc.lostfound.auth.vo.RolePermissionSaveVO;
import com.zmc.lostfound.common.utils.Result;
import com.zmc.lostfound.common.valid.AddGroup;
import com.zmc.lostfound.common.valid.UpdateGroup;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author GXC
 * @description 角色&权限关联信息管理接口
 * @email 2869581855@qq.com
 * @date 2023-07-05 15:09:50
 */
@RestController
@RequestMapping("/rolePermission")
@Api(value = "角色&权限关联信息管理接口", tags = "角色&权限关联信息管理接口")
public class RolePermissionController {

    private final RolePermissionService rolePermissionService;

    public RolePermissionController(RolePermissionService rolePermissionService) {
        this.rolePermissionService = rolePermissionService;
    }

    @ApiOperation("新增角色&权限关联信息")
    @PostMapping("/insertRolePermission")
    public Result insertRolePermission(@RequestBody @Validated(value = {AddGroup.class}) RolePermissionSaveVO rolePermissionSaveVO) {
        return rolePermissionService.insertRolePermission(rolePermissionSaveVO);
    }

    @ApiOperation("修改角色&权限关联信息")
    @PutMapping("/updateRolePermission")
    public Result updateRolePermission(@RequestBody @Validated(value = {UpdateGroup.class}) RolePermissionSaveVO rolePermissionSaveVO) {
        return rolePermissionService.updateRolePermission(rolePermissionSaveVO);
    }

    @ApiOperation("按主键ids批量删除角色&权限关联信息")
    @DeleteMapping("/deleteBatchByIds")
    public Result deleteBatchByIds(@RequestBody List<Long> ids) {
        return rolePermissionService.deleteBatchByIds(ids);
    }

    @ApiOperation("按角色id批量删除角色&权限关联信息")
    @DeleteMapping("/deleteBatchByRoleId/{roleId}")
    public Result deleteBatchByRoleId(@PathVariable("roleId") Long roleId) {
        return rolePermissionService.deleteByRoleIdAndPermissionId(roleId, null);
    }

    @ApiOperation("按权限id批量删除角色&权限关联信息")
    @DeleteMapping("/deleteBatchByPermissionId/{permissionId}")
    public Result deleteBatchByPermissionId(@PathVariable("permissionId") Long permissionId) {
        return rolePermissionService.deleteByRoleIdAndPermissionId(null, permissionId);
    }

    @ApiOperation("按角色id和权限id删除角色&权限关联信息")
    @DeleteMapping("/delete/{roleId}/and/{permissionId}")
    public Result deleteByRoleIdAndPermissionId(@PathVariable("roleId") Long roleId, @PathVariable("permissionId") Long permissionId) {
        return rolePermissionService.deleteByRoleIdAndPermissionId(roleId, permissionId);
    }

}
